{ *******************************************************************************
  Copyright 2010-2011 Daniele Teti

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  ******************************************************************************** }

unit dorm.adapter.ADO.Factory;

interface

uses
  {$IF CompilerVersion > 22}
  Data.DB, Data.Win.ADODB,
  {$ELSE}
  AdoDb,
  {$IFEND}
  superobject,
  dorm.Commons;

type
  TADOFactory = class
  protected
  var
    FADOConnection: TADOConnection;
    FADOQuery: TADOQuery;
  private
    procedure Configure(ConfigurationInfo: ISuperObject);
  protected
    database_connection_string: string;
  public
    constructor Create(ConfigurationInfo: ISuperObject; ConnectionString: String = '');
    destructor Destroy; override;
    function GetConnection: TADOConnection;
    function GetQuery: TADOQuery;
    function GetLastKeyInserted: Integer;
  end;

implementation

uses
  sysutils;

{ Factory }

procedure TADOFactory.Configure(ConfigurationInfo: ISuperObject);
begin
  //*-----------------------------------------------*
  //* Retrieve Connection String from Configuration *
  //*-----------------------------------------------*
  database_connection_string := ConfigurationInfo.S['database_connection_string'];
end;

constructor TADOFactory.Create(ConfigurationInfo: ISuperObject; ConnectionString: String = '');
begin
  inherited Create;
  //*----------------------------------------------------------------------------------------*
  //* Retrieve connection string from configuration file or get to connectionstring variable *
  //*----------------------------------------------------------------------------------------*
  if ConfigurationInfo<>nil then
    Configure(ConfigurationInfo)
  else
    database_connection_string := ConnectionString;
  //*-----------------------*
  //* Create ADO Connection *
  //*-----------------------*
  FADOConnection := TADOConnection.Create(nil);
  FADOConnection.ConnectionString := database_connection_string;
  //*--------------------*
  //* Remove LoginPrompt *
  //*--------------------*
  FADOConnection.LoginPrompt := False;
  //*-----------------*
  //* Open Connection *
  //*-----------------*
  FADOConnection.Open;
  //*------------------*
  //* Create ADO Query *
  //*------------------*
  FADOQuery := TADOQuery.Create(nil);
  FADOQuery.Connection := FADOConnection;
end;

destructor TADOFactory.Destroy;
begin
  //*-----------------------------*
  //* Close and Destroy ADO Query *
  //*-----------------------------*
  if Assigned(FADOQuery) then
  begin
    if FADOQuery.Active then FADOQuery.Close;
    FADOQuery.Free;
  end;
  //*----------------------------------*
  //* Close and Destroy ADO Connection *
  //*----------------------------------*
  if Assigned(FADOConnection) then
  begin
    if FADOConnection.Connected then FADOConnection.Close;
    FADOConnection.Free;
  end;
  inherited;
end;

function TADOFactory.GetConnection: TADOConnection;
begin
  Result := FADOConnection;
end;

function TADOFactory.GetLastKeyInserted: Integer;
begin
  //*---------------------------*
  //* Retrieve Last Id Inserted *
  //*---------------------------*
  FADOQuery.Sql.Clear;
  FADOQuery.SQL.Add('SELECT @@IDENTITY AS LASTKEY');
  FADOQuery.Open;
  Result := FADOQuery['LASTKEY'];
  FADOQuery.Close;
end;

function TADOFactory.GetQuery: TADOQuery;
begin
  Result := FADOQuery;
end;

end.
